package org.dyz.banana.platform.uaa.controller;

import java.io.IOException;
import java.sql.Timestamp;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.dyz.banana.platform.common.service.ITUserService;
import org.dyz.banana.platform.common.vo.HttpResult;
import org.dyz.banana.platform.common.vo.TUserVo;
import org.dyz.banana.platform.uaa.util.JwtAuthenticatioToken;
import org.dyz.banana.platform.uaa.util.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@Api(value = "登录控制器")
@RestController
public class LoginController {

    @Autowired
    private AuthenticationManager authenticationManager;

    @Autowired
    private ITUserService userService;
    /**
     * 登录接口
     */
    @ApiOperation(value = "登录方法", notes = "")
    @PostMapping(value = "/login")
    public HttpResult login(
    		@ApiParam(value = "登录入参" , required=true )
    		@RequestBody TUserVo loginBean, HttpServletRequest request) throws IOException {
        String username = loginBean.getUserName();
        String password = loginBean.getPassword();
        String msg = null;
    	if((msg = validate(loginBean))!=null) {
    		return HttpResult.error(msg);
        }
        // 系统登录认证
        JwtAuthenticatioToken token = SecurityUtils.login(request, username, password, authenticationManager);
                
        return HttpResult.ok(token);
    }
    
    /**
     * 注册接口
     */
    @ApiOperation(value = "注册方法", notes = "")
    @PostMapping(value = "/register")
    public HttpResult register(
    		@ApiParam(value = "注册入参" , required=true )
    		@RequestBody TUserVo registerBean, HttpServletRequest request) throws IOException {
    	String msg = null;
    	if((msg = validate(registerBean))!=null) {
    		return HttpResult.error(msg);
        }
    	PasswordEncoder encoder = new BCryptPasswordEncoder();
    	String ecodedPassword = encoder.encode(registerBean.getPassword());
    	registerBean.setPassword(ecodedPassword);
    	registerBean.setDefunInd(false);
    	registerBean.setCreatedTimestamp(new Timestamp(new Date().getTime()));
        boolean result = userService.save(registerBean);
        if(result) {
        	return HttpResult.ok();
        }else {
        	return HttpResult.error();
        }
        
    }
    
    
    private String validate(TUserVo bean) {
    	if(StringUtils.isEmpty(bean.getUserName())) {
    		return "用户名不能为空";
    	}
    	return null;
    }

}
